package com.media.themovieworld.util;

import java.util.HashMap;

import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.api.ApiRequest;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class SmsUtils {
	private Context mconContext;
	private String keyword,shorCode;
	public SmsUtils(Context context) {
		this.mconContext = context;
	}
	
	private void getpayment(String shortCode,String keyword, String status) {
		TelephonyManager tManager = (TelephonyManager) mconContext.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		String mPhoneNumber = tManager.getLine1Number();
		final HashMap<String, String> map = new HashMap<String, String>();
		map.put("deviceImei", deviceIMEI);
		map.put("versionId", "1");
		map.put("shortCode", shortCode);
		map.put("keyword", keyword);
		map.put("status", status);
		map.put("mobile", mPhoneNumber);
		map.put("description", "");
		ApiRequest.sendRequest(map, ApiParams.GET_PAYMENT);
		 
	}
	
	public void sendSMS(final String phoneNumber, final String message) {
		keyword = message;
		shorCode = phoneNumber;
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPI = PendingIntent.getBroadcast(mconContext, 0,new Intent(SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(mconContext, 0,new Intent(DELIVERED), 0);

		// ---when the SMS has been sent---
		mconContext.registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(mconContext, "SMS sent", Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(mconContext, "Generic failure",Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(mconContext, "No service",Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(mconContext, "Null PDU", Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(mconContext, "Radio off", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		// ---when the SMS has been delivered---
		mconContext.registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(mconContext, "SMS delivered",Toast.LENGTH_SHORT).show();
					new getPaymentTask().execute("1");
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(mconContext, "SMS not delivered",Toast.LENGTH_SHORT).show();
					new getPaymentTask().execute("0");
					break;
				}
			}
		}, new IntentFilter(DELIVERED));
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}
	
	private class getPaymentTask extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			getpayment(shorCode, keyword, params[0]);
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}
		
	}
}
